package org.example.doris;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.StopWatch;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicLong;

@Slf4j
public class TagTask implements Runnable{

    private DruidDataSource dataSource;

    private static final AtomicLong userStart = new AtomicLong(320281199508233775l);

    private static final Long userSize = 6000000l;

    private static final Integer tagSize = 250;

    private static final List<String> userList = new ArrayList<>();

    private static final List<String> tagList = new ArrayList<>();

    private static final Integer batchSize = 10000;

    private static CountDownLatch latch = null;

    private static final StopWatch stopwatch = new StopWatch();

    public TagTask(DruidDataSource dataSource, CountDownLatch latch) {
        this.dataSource = dataSource;
        this.latch = latch;

        // 用户身份证
        stopwatch.start();
        for (int i = 0; i < userSize; i++) {
            userList.add(String.valueOf(userStart.getAndIncrement()));
        }
        stopwatch.stop();
        log.info("初始化用户耗时：{}毫秒", stopwatch.getTime());

        // 标签名字
        stopwatch.reset();
        stopwatch.start();
        for (int i = 1; i <= tagSize; i++) {
            tagList.add("tag" + i);
        }
        stopwatch.stop();
        log.info("初始化标签耗时：{}毫秒", stopwatch.getTime());
    }

    @Override
    public void run() {

        String sql = String.format("insert into ep_taglist values(?, ?, to_bitmap(?))");
        long batchNum = userSize / batchSize;
        try {

            // 250个标签
            for (int i = 1; i <= tagSize; i++) {

                for (int j = 0; j < batchNum; j++) {
                    // 每次1万条提交一次
                    stopwatch.reset();
                    stopwatch.start();
                    DruidPooledConnection connection = dataSource.getConnection();
                    PreparedStatement statement = connection.prepareStatement(sql);
                    connection.setAutoCommit(false);
                    for (int k = 0; k < batchSize; k++) {
                        statement.setObject(1, i);
                        statement.setObject(2, tagList.get(i));
                        statement.setObject(3, userList.get(k + j * batchSize));
                        statement.addBatch();
                    }
                    statement.executeBatch();
                    statement.clearBatch();
                    connection.commit();
                    statement.close();
                    connection.close();
                    stopwatch.stop();
                    log.info("{}条耗时：{}", batchSize, stopwatch.getTime());
                }
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        latch.countDown();
    }
}
